/**
 * 新增合同
 * @author 孙汉斌
 */
package com.zjht.gmcc.servlet.order;

import java.io.PrintWriter;
import java.sql.PreparedStatement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.action.OrderUtil;
import com.zjht.gmcc.bean.util.MyFormat;
import com.zjht.gmcc.bean.util.MyJDBC;

public class AddOrder extends BaseServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected boolean process(HttpServletRequest request, HttpServletResponse response, HttpSession session, PrintWriter out, MyJDBC myJDBC,
			String menu) throws Exception {

		String s_providerID = request.getParameter("providerid");// 供应商ID
		String s_ordercode = request.getParameter("ordercode");// 合同编号
		String[] producttypeids = request.getParameterValues("producttypeid");// 合作范围
		String s_ordertype = request.getParameter("ordertype");// 采购类型
		String s_orderyear = request.getParameter("orderyear");// 合同年份
		String s_startdate = request.getParameter("startdate");// 有效期(起始日期)
		String s_enddate = request.getParameter("enddate");// 有效期(截止日期)
		String s_signoper = request.getParameter("signoper");// 签约人
		String s_memo = request.getParameter("memo");// 备注说明
        String s_status="";//合同状态
        if(MyFormat.getDayInterval(MyFormat.getToday(), s_startdate)>0||MyFormat.getDayInterval(s_enddate,MyFormat.getToday())>0){//开始日期大于当日或截止日期小于当日
        	s_status=OrderUtil.ORDER_STATUS_STOP;
        }else if(MyFormat.getDayInterval(s_enddate,MyFormat.getToday())<=30 && MyFormat.getDayInterval(s_enddate,MyFormat.getToday())>=0){//快到期
        	s_status=OrderUtil.ORDER_STATUS_SOON_STOP;
        }else{
        	s_status=OrderUtil.ORDER_STATUS_OK;
        }
        
		// 数据插入Orders表
		StringBuilder sql = new StringBuilder();
		PreparedStatement ps = null;
		int flag = 0;
		sql
				.append("INSERT INTO ORDERS(orderid,ordercode,providerid,ordertypeid,orderyear,orderrange,startdate,enddate,signoper,memo,status)VALUES(SEQ_ORDER_ID.NEXTVAL,?,?,?,?,?,?,?,?,?,?)");
		ps = myJDBC.prepareStatement(sql);
		ps.setString(1, s_ordercode);
		ps.setString(2, s_providerID);
		ps.setString(3, s_ordertype);
		ps.setString(4, s_orderyear);
		ps.setString(6, s_startdate);
		ps.setString(7, s_enddate);
		ps.setString(8, s_signoper);
		ps.setString(9, s_memo);
		ps.setString(10, s_status);
		String s_producttypeid = "";
		for (int i = 0; i < producttypeids.length; i++) {
			if (s_producttypeid.length() > 0)
				s_producttypeid += ",";
			s_producttypeid += producttypeids[i];
		}
		ps.setString(5, s_producttypeid);
		flag = ps.executeUpdate();
		ps.close();
		ps = null;

		// 修改Provider表对应的orderstatus
		if (flag == 1) {
			flag = 0;
			sql = new StringBuilder();
			sql.append("UPDATE Provider SET orderstatus='1' WHERE providerid=?");
			ps = myJDBC.prepareStatement(sql);
			ps.setString(1, s_providerID);
			flag = ps.executeUpdate();
		}
		ps.close();
		ps = null;

		String tip = "新增合同";
		if (flag == 1) {
			alert(out, tip + "成功", "/order/order.jsp");
		} else {
			errAlert(request, out, menu, tip + "失败");
		}
		return flag == 1;
	}
}
